//
//  LoggingTable.m
//  Daily Wallpaper Changer for Bing
//
//  Created by 杨岑 on 11/3/14.
//  Copyright (c) 2014 杨岑. All rights reserved.
//

#import "TextTableBuilder.h"
#import "NSMutableAttributedString+Ext.h"


#define N_TIMESTAMP_WIDTH_PERCENT   26
#define N_TIMESTAMP_WIDTH           48


@interface TextTableBuilder ()
@property (nonatomic) NSString* timestampTitle;
@property (nonatomic) NSString* logTextTitle;
@end


@implementation TextTableBuilder
{
    NSMutableAttributedString* _headerRowAttrString;
    NSTextTable* _table;
}

- (instancetype)initWith:(NSString *)timestampTitle logTitle:(NSString *)logTitle
{
    self = [super init];
    if (self)
    {
        self.timestampTitle = timestampTitle;
        self.logTextTitle = logTitle;
        [self setupTable];
    }
    return self;
}

+ (instancetype)createWithTitle:(NSString *)timestampTitle logTitle:(NSString *)logTitle
{
    return [[TextTableBuilder alloc] initWith:timestampTitle logTitle:logTitle];
}

- (void)setupTable
{
    _table = [[NSTextTable alloc] init];
    [_table setNumberOfColumns:2];
    
    _headerRowAttrString = [self addRow:_timestampTitle logText:_logTextTitle textColor:[NSColor controlTextColor]];
}

- (NSMutableAttributedString*)addRow:(NSString *)timestamp logText:(NSString *)log textColor:(NSColor *)color
{
    NSMutableAttributedString* cellAttrString;
    NSMutableAttributedString* rowAttrString = [[NSMutableAttributedString alloc] init];
    
    cellAttrString = [self createCellAt:0 withText:timestamp textColor:color];
    [rowAttrString appendAttributedString:cellAttrString];
    
    cellAttrString = [self createCellAt:1 withText:log textColor:color];
    [rowAttrString appendAttributedString:cellAttrString];
    
    _numRows++;
    return rowAttrString;
}

- (NSMutableAttributedString*)createCellAt:(NSInteger)column withText:(NSString *)text textColor:(NSColor*)color
{
    NSInteger rowNo = _numRows;
    NSTextTableBlock *block = [[NSTextTableBlock alloc] initWithTable:_table startingRow:rowNo rowSpan:1 startingColumn:column columnSpan:1];

    [block setWidth:1.0 type:NSTextBlockAbsoluteValueType forLayer:NSTextBlockBorder];
    if (column == 0)
    {
        //[block setValue:N_TIMESTAMP_WIDTH_PERCENT type:NSTextBlockPercentageValueType forDimension:NSTextBlockWidth];
        [block setValue:N_TIMESTAMP_WIDTH type:NSTextBlockAbsoluteValueType forDimension:NSTextBlockWidth];
    }
    if (rowNo == 0)
    {
        [block setBackgroundColor:[NSColor controlColor]];
        [block setBorderColor:[NSColor controlShadowColor]];
        [block setWidth:1.0 type:NSTextBlockAbsoluteValueType forLayer:NSTextBlockPadding];
    }
    else
    {
        NSArray* rowColors = [NSColor controlAlternatingRowBackgroundColors];
        [block setBackgroundColor:rowColors[(rowNo - 1) % rowColors.count]];
    }
    
    NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragraphStyle setTextBlocks:@[block]];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    
    NSDictionary* colorAttrDic = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    
    static NSAttributedString* NL;
    
    if (!NL)
    {
        NL = [[NSAttributedString alloc] initWithString:@"\n" attributes:nil];
    }
    
    NSMutableAttributedString* cellAttrString;
    if (column == 0)
    {
        cellAttrString = [NSMutableAttributedString attachmentFromImage:[NSImage imageNamed:NSImageNameComputer]];
    }
    else
    {
        cellAttrString = [[NSMutableAttributedString alloc] initWithString:text attributes:colorAttrDic];
        NSRange all = NSMakeRange(0, cellAttrString.length);
        [cellAttrString addAttribute:NSFontAttributeName value:[NSFont userFontOfSize:32] range:all];
    }
    
    [cellAttrString appendAttributedString:NL];
    [cellAttrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [cellAttrString length])];
    
    return cellAttrString;
}


@end
